package com.cmas.manage.service.impl;

import com.cmas.common.core.domain.entity.SysDept;
import com.cmas.manage.mapper.ActivityMapper;
import com.cmas.manage.mapper.CommunityMapper;
import com.cmas.manage.mapper.StudentInfoMapper;
import com.cmas.manage.mapper.TeacherInfoMapper;
import com.cmas.manage.service.ICountDataService;
import com.cmas.manage.vo.CountDataVo;
import com.cmas.system.mapper.SysDeptMapper;
import com.cmas.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author heyanghui
 */

@Service
public class CountDataServiceImpl implements ICountDataService {

    @Autowired
    private StudentInfoMapper studentInfoMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TeacherInfoMapper teacherInfoMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;


    @Override
    public Integer stuCount() {
        return studentInfoMapper.stuCount();
    }

    @Override
    public Integer userCount() {
        return sysUserMapper.userCount();
    }

    @Override
    public Integer teacherCount() {
        return teacherInfoMapper.teacherCount();
    }

    @Override
    public Integer actCount() {
        return activityMapper.actCount();
    }

    @Override
    public Integer communityCount() {
        return communityMapper.communityCount();
    }

    @Override
    public Integer collegeCount() {
        return sysDeptMapper.collegeCount();
    }

    @Override
    public List<CountDataVo> ccCount() {
        return communityMapper.ccCount();
    }

    @Override
    public List<CountDataVo> cActCount() {
        return activityMapper.cActCount();
    }

    @Override
    public List<CountDataVo> cUserCount() {
        List<Long> longs = communityMapper.selectDeptId();
        return getCountDataVos(longs);
    }

    private List<CountDataVo> getCountDataVos(List<Long> longs) {
        List<String> list = new ArrayList<>();
        for (Long aLong : longs) {
            String deptName = sysDeptMapper.selectColleg(aLong);
            if(deptName != null){
                list.add(deptName);
            }
        }
        List<CountDataVo> list1 = new ArrayList<>();
//        if(list.size() == 0){
//            return list1;
//        }
        // 分组统计
        Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(String::toString, Collectors.counting()));
        Set<String> strings = collect.keySet();
        for (String string : strings) {
            CountDataVo countDataVo = new CountDataVo();
            countDataVo.setName(string);
            countDataVo.setValue(collect.get(string).intValue());
            list1.add(countDataVo);
        }
        return list1;
    }

    @Override
    public List<CountDataVo> cStuCount() {
        List<Long> longs = studentInfoMapper.selectDeptId();
        return getCountDataVos(longs);
    }
    @Override
    public List<CountDataVo> cFirstCount() {
        List<Long> longs = activityMapper.selectPrizeDeptId("1");
        return getCountDataVos(longs);
    }
    @Override
    public List<CountDataVo> cSecondCount() {
        List<Long> longs = activityMapper.selectPrizeDeptId("2");
        return getCountDataVos(longs);
    }
    @Override
    public List<CountDataVo> cThirdCount() {
        List<Long> longs = activityMapper.selectPrizeDeptId("3");
        return getCountDataVos(longs);
    }
}
